<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html><head><title>Programming in Lua : 15</title>
<link rel="stylesheet" type="text/css" href="pil.css">
</head>
<body>
<p class="warning">
<A HREF="contents.html"><IMG TITLE="Programming in Lua (first edition)" SRC="capa.jpg" ALT="" ALIGN="left"></A>This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.<BR>The third edition targets Lua 5.2 and is available at <A HREF="http://www.amazon.com/exec/obidos/ASIN/859037985X/theprogrammil3-20">Amazon</A> and other bookstores.<BR>By buying the book, you also help to <A HREF="../donations.html">support the Lua project</A>.
</p>
<table width="100%" class="nav">
<tr>
<td width="10%" align="left"><a href="14.3.html"><img border="0" src="left.png" alt="Previous"></a></td>
<td width="80%" align="center"><a href="contents.html"><font face="Helvetica,Arial,sanserif">
<font color="gray">Programming in </font><font color="blue"> Lua</font>
</font></a></td>
<td width="10%" align="right"><a href="15.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
<tr>
<td width="10%" align="left"></td>
<td width="80%" align="center"><a href="contents.html#P2">Part II. Tables and Objects</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="contents.html#15">Chapter 15. Packages</a></td>
<td width="10%" align="right"></td></tr>
</table>
<hr/>
<h1>15 &ndash; Packages</h1>

<p>Many languages provide mechanisms to organize their space of global names,
such as <em>modules</em> in Modula,
<em>packages</em> in Java and Perl,
or <em>namespaces</em> in C++.
Each of these mechanisms has different rules regarding the
use of elements declared inside a package,
visibility, and other details.
Nevertheless, all of them provide a basic mechanism
to avoid collision among names defined in different libraries.
Each library creates its own namespace
and names defined inside this namespace do not interfere
with names in other namespaces.

<p>Lua does not provide any explicit mechanism for packages.
However, we can implement them easily with the basic
mechanisms that the language provides.
The main idea is to represent each package by a table,
as the basic libraries do.

<p>An obvious benefit of using tables to implement packages
is that we can manipulate packages like any other table
and use the whole power of Lua to create extra facilities.
In most languages, packages are not first-class values
(that is, they cannot be stored in variables,
passed as arguments to functions, etc.),
so these languages need special mechanisms for each extra trick
you may do with a package.

<p>In Lua, although we always represent packages as tables,
there are several different methods to write a package.
In this chapter, we cover some of these methods.

<hr/>
<table width="100%" class="nav">
<tr valign="top">
<td width="90%" align="left">
<small>
  Copyright &copy; 2003&ndash;2004 Roberto Ierusalimschy.  All rights reserved.
</small>
</td>
<td width="10%" align="right"><a href="15.1.html"><img border="0" src="right.png" alt="Next"></a></td>
</tr>
</table>


</body></html>

